package com.zeros.myself.practice_delete.practice3

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

/**
 * @author zqq on 2019/12/30.
 */
class Sample14GetFontMetricsView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    internal var paint1 = Paint(Paint.ANTI_ALIAS_FLAG)
    internal var paint2 = Paint(Paint.ANTI_ALIAS_FLAG)

    internal var texts = arrayOf("A", "a", "J", "j", "Â", "â")
    internal var yOffset: Float = 0.toFloat()
    internal var top = 200
    internal var bottom = 400

    //FontMetrics 是个相对专业的工具类，它提供了几个文字排印方面的数 值：ascent, descent, top, bottom, leading。baseline


    init {
        paint1.style = Paint.Style.STROKE
        paint1.strokeWidth = 20f
        paint1.color = Color.parseColor("#E91E63")

        paint2.textSize = 160f

        val fontMetrics: Paint.FontMetrics = paint2.fontMetrics
        yOffset = -(fontMetrics.ascent + fontMetrics.descent) / 2
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawRect(50f, top.toFloat(), width - 50f, bottom.toFloat(), paint1)

        val middle = (top + bottom) / 2

        canvas.drawText(texts[0], 100f, middle + yOffset, paint2)
        canvas.drawText(texts[1], 200f, middle + yOffset, paint2)
        canvas.drawText(texts[2], 300f, middle + yOffset, paint2)
        canvas.drawText(texts[3], 400f, middle + yOffset, paint2)
        canvas.drawText(texts[4], 500f, middle + yOffset, paint2)
        canvas.drawText(texts[5], 600f, middle + yOffset, paint2)

    }

}